package myQueue;

public class MyQueue {
    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    ListNode front;
    ListNode rear;
    int usedsize = 0;

    public void offer(int data){
        //头插法
        ListNode node = new ListNode(data);
        if(front == null) {
            front = rear = node;
        } else {
            front.prev = node;
            node.next = front;
            front = node;
        }
        usedsize++;
    }
    public int poll(){
        //从尾部删除元素
        if(front == null){
            return -1;
        }
        int ret = rear.val;
        if(front == rear){
            front = null;
            rear = null;
            usedsize--;
            return ret;
        }
        rear = rear.prev;
        rear.next = null;
        usedsize--;
        return ret;
    }
    public int peek(){
        if(front == null){
            return -1;
        }
        return rear.val;
    }
    public int size(){
        return this.usedsize;
    }
    public boolean isEmpty(){
        return usedsize == 0;
    }
}
